package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseFlowmeterVo;
import com.workcell.workdata.xthings.entity.BaseFlowmeter;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseFlowmeterService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流量计型号
 *
 * @author 廖博文
 * @date 2022-11-28 10:15:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/baseflowmeter" )
@Api(value = "baseflowmeter", tags = "流量计型号管理")
public class BaseFlowmeterController {

    private final  BaseFlowmeterService baseFlowmeterService;
    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseFlowmeter 流量计型号
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getBaseFlowmeterPage(Page page, BaseFlowmeter baseFlowmeter) {
        return R.ok(baseFlowmeterService.page(page, Wrappers.query(baseFlowmeter)));
    }

	/**
	 * 模糊分页查询
	 * @param baseFlowmeterVo
	 * @return
	 */
	@ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
	@GetMapping("/vaguePage" )
	public R getBaseFlowmeterVaguePage(BaseFlowmeterVo baseFlowmeterVo) {
		return R.ok(baseFlowmeterService.getBaseFlowmeterVaguePage(baseFlowmeterVo));
	}

    /**
     * 通过id查询流量计型号
     * @param  fmId fmId
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{fmId}" )
    public R getById(@PathVariable("fmId") String fmId) {
        return R.ok(baseFlowmeterService.getById(fmId));
    }

    /**
     * 新增流量计型号
     * @param baseFlowmeter 流量计型号
     * @return R
     */
    @ApiOperation(value = "新增流量计型号", notes = "新增流量计型号")
    @PostMapping
    public R save(@RequestBody BaseFlowmeter baseFlowmeter) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseFlowmeter.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseFlowmeter.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseFlowmeter.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseFlowmeter.setManufacturer(manufacturer.getManufacturerFullName());
		baseFlowmeter.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseFlowmeterService.save(baseFlowmeter));
    }

    /**
     * 修改流量计型号基本信息
     * @param baseFlowmeter 流量计型号
     * @return R
     */
    @ApiOperation(value = "修改流量计型号基本信息", notes = "修改流量计型号基本信息")
    @PutMapping
    public R updateBaseFlowmeter(@RequestBody BaseFlowmeter baseFlowmeter) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseFlowmeter.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseFlowmeter.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseFlowmeter.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseFlowmeter.setManufacturer(manufacturer.getManufacturerFullName());
		baseFlowmeter.setManufacturerCode(manufacturer.getManufacturerCode());
		return R.ok(baseFlowmeterService.updateBaseFlowmeter(baseFlowmeter));
    }

	/**
	 * 修改流量计型号状态信息
	 * @param baseFlowmeter 流量计型号
	 * @return R
	 */
	@ApiOperation(value = "修改流量计型号状态信息", notes = "修改流量计型号状态信息")
	@PutMapping("/updateStateById")
	public R updateStateById(@RequestBody BaseFlowmeter baseFlowmeter) {
		return R.ok(baseFlowmeterService.updateStateById(baseFlowmeter));
	}

    /**
     * 通过id删除流量计型号
     * @param fmId fmId
     * @return R
     */
    @ApiOperation(value = "通过id删除流量计型号", notes = "通过id删除流量计型号")
    @DeleteMapping("/{fmId}" )
    public R removeById(@PathVariable("fmId") String fmId) {
        return R.ok(baseFlowmeterService.delBaseFlowmeterById(fmId));
    }


    /**
     * 导出excel 表格
     * @param baseFlowmeter 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseFlowmeter> export(BaseFlowmeter baseFlowmeter) {
        return baseFlowmeterService.list(Wrappers.query(baseFlowmeter));
    }

}
